home *** CD-ROM | disk | FTP | other *** search
/ MacHack 1994 / MacHack 1994.toast / MacHack™94 / Talks & Papers / Timothy Knox / yerk 3.66 / Asm source / Dictionary < prev    next >
Text File  |  1994-06-24  |  4KB  |  143 lines

  1. \ DICTIONARY CLASS                  Reese Warner                4/85
  2. \  Useful for symbol tables, et. al.
  3.  
  4. 1 -> dlevel
  5.  
  6. :CLASS DictElt          <super String
  7.  
  8.     Var     Ptr     \ pointer to the next element in the collision 
  9.                     \  resolution chain
  10.     Var     Info    \ contains the data associated with the word
  11.  
  12.     :M PUTDATA: put: info ;M
  13.  
  14.     :M GETDATA: get: info ;M
  15.  
  16.     :M SETNEXT: put: ptr  ;M
  17.  
  18.     :M GETNEXT: get: ptr  ;M
  19.  
  20.     :M DISPOSE:
  21.         getNext: self 0= not
  22.         IF
  23.             dispose: [ getNext: self ]
  24.             dispose: ptr
  25.         THEN   ;M
  26.  
  27. ;CLASS
  28.  
  29. :CLASS Dictionary  <super Array
  30.  
  31. \ CHAIN - does the main work of methods ENTER & QUERY. Intended to be a private
  32. \   method for class DICTIONARY. Searchs the chain at a given bucket for
  33. \   string. If String not found then returns false, otherwise true. Objaddr
  34. \   points to last element in chain.
  35.     :M CHAIN:   { objAddr strObj \ oldAddr flag -- objAddr bool }
  36.         FALSE -> flag
  37.         BEGIN
  38.             get: strObj get: objAddr s=
  39.             IF
  40.                 TRUE -> flag
  41.             THEN
  42.             objAddr -> oldAddr
  43.             getnext: objAddr -> objAddr
  44.             objAddr 0= flag or
  45.         UNTIL
  46.         oldAddr flag
  47.     ;M
  48.  
  49. \ QUERY - returns value associated with String in dictionary. If string is
  50. \  not found in dictionary, then 0 is returned.
  51.     :M QUERY:   { strObj \ idx checkAddr val -- val }
  52.         get: strObj str255 -base hash limit: self mod -> idx
  53.         idx at: self -> checkAddr
  54.         checkAddr 0=
  55.         IF
  56.             0 -> Val
  57.         ELSE
  58.             checkAddr strObj chain: self swap -> checkAddr
  59.             IF
  60.                 getData: checkAddr -> val
  61.             ELSE
  62.                 0 -> val
  63.             THEN
  64.         THEN
  65.         val
  66.     ;M
  67.  
  68. \ ENTER - puts the string and value into the dictionary. If the string is
  69. \  already in the dictionary then it changes the value to the new value.
  70.     :M ENTER: { val strObj \ idx EltAddr OldAddr -- }
  71.         get: strObj str255 -base hash limit: super mod -> idx
  72.         idx at: super -> oldAddr 
  73.         oldAddr 0=
  74.         IF
  75.             Heap> DictElt -> EltAddr new: EltAddr
  76.             get: strObj put: EltAddr
  77.             val putData: EltAddr
  78.             EltAddr idx to: super
  79.         ELSE
  80.             oldAddr strObj chain: self swap -> oldAddr
  81.             IF
  82.                 val putData: oldAddr
  83.             ELSE
  84.                 heap> DictElt -> EltAddr new: EltAddr
  85.                 get: strObj put: EltAddr
  86.                 val putData: EltAddr
  87.                 EltAddr setNext: oldAddr
  88.             THEN
  89.         THEN
  90.     ;M
  91.  
  92. \ EXEC - for every element in the dictionary exec: executes routine with the 
  93. \  associated value and an addr/len pair representing the hashed string as 
  94. \  an argument
  95.     :M EXEC:    { routine \ objAddr -- }
  96.         limit: self 0 
  97.         DO
  98.             i at: self -> objAddr
  99.             objAddr 0= not
  100.             IF
  101.                 BEGIN
  102.                     getData: objAddr get: objAddr exec> routine
  103.                     getNext: objAddr -> objAddr
  104.                     objAddr 0=
  105.                 UNTIL
  106.             THEN
  107.         LOOP
  108.     ;M
  109.  
  110.     :M DISPOSE:
  111.         limit: super 0
  112.         DO
  113.             i at: super 0= not
  114.             IF
  115.                 dispose: [ i at: super ]
  116.                 i dispose: super
  117.             THEN
  118.         LOOP
  119.     ;M
  120.  
  121. ;CLASS
  122.  
  123. :CLASS Symbols <SUPER Dictionary
  124. \ QUERY - returns value associated with String in dictionary and FoundFlag. If
  125. \ string is not found in dictionary, then 0 0 is returned. Needed for SymTab
  126. \ where 0 is valid value.
  127.     :M QUERY:   { strObj \ idx checkAddr val found -- val found }
  128.         FALSE -> Found
  129.         get: strObj str255 -base hash limit: self mod -> idx
  130.         idx at: self -> checkAddr
  131.         checkAddr
  132.         IF
  133.             checkAddr strObj chain: self -> Found -> checkAddr
  134.             Found
  135.             IF
  136.                 getData: checkAddr -> val
  137.             THEN
  138.         THEN
  139.         val Found
  140.     ;M
  141.  
  142. ;CLASS
  143.